<!-- 横向布局 -->
<template>
  <ElContainer class="layout">
    <ElHeader>
      <div class="logo flx-center">
        <img class="logo-img" src="@/assets/images/logo.png" alt="logo" />
        <span class="logo-text">{{ title }}</span>
      </div>
      <ElMenu mode="horizontal" :router="false" :default-active="activeMenu">
        <!-- 不能直接使用 SubMenu 组件，无法触发 el-menu 隐藏省略功能 -->
        <template v-for="subItem in menuList" :key="subItem.name">
          <ElSubMenu v-if="subItem.children?.length" :key="subItem.name" :index="subItem.name + 'el-sub-menu'">
            <template #title>
              <ElIcon>
                <component :is="subItem.meta.icon"></component>
              </ElIcon>
              <span>{{ subItem.meta.title }}</span>
            </template>
            <SubMenu :menu-list="subItem.children" />
          </ElSubMenu>
          <ElMenuItem v-else :key="subItem.name + 'el-menu-item'" :index="subItem.name" @click="handleClickMenu(subItem)">
            <ElIcon>
              <component :is="subItem.meta.icon"></component>
            </ElIcon>
            <template #title>
              <span>{{ subItem.meta.title }}</span>
            </template>
          </ElMenuItem>
        </template>
      </ElMenu>
      <ToolBarRight />
    </ElHeader>
    <Main />
  </ElContainer>
</template>

<script setup lang="ts" name="layoutTransverse">
import { computed } from "vue";
import { ElContainer, ElHeader, ElIcon, ElSubMenu, ElMenu, ElMenuItem } from "element-plus";
import { useAuthStore } from "@/stores/modules/auth";
import { useRoute, useRouter } from "vue-router";
import Main from "@/layouts/components/Main/index.vue";
import ToolBarRight from "@/layouts/components/Header/ToolBarRight.vue";
import SubMenu from "@/layouts/components/Menu/SubMenu.vue";
import { urlParamToJson } from "@/utils";
const title = import.meta.env.VITE_GLOB_APP_TITLE;

const route = useRoute();
const router = useRouter();
const authStore = useAuthStore();
const menuList = computed(() => authStore.showMenuListGet);
const activeMenu = computed(() => route.name as string);

const handleClickMenu = (subItem: Menu.MenuOptions) => {
  if (subItem.meta.isLink) {
    if (subItem.meta.param) {
      return window.open(subItem.path + "?" + subItem.meta.param, "_blank");
    } else {
      return window.open(subItem.path as string, "_blank");
    }
  } else {
    if (subItem.meta.param) {
      router.push({ path: subItem.path, query: urlParamToJson(subItem.meta.param) });
    } else {
      router.push(subItem.path);
    }
  }
};
</script>

<style scoped lang="scss">
@import "./index.scss";
</style>
